package net.wofly.cashier.room.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import net.wofly.cashier.room.domain.Room;
import net.wofly.cashier.room.repository.RoomRepository;
import net.wofly.cashier.room.service.IRoomService;
import net.wofly.cashier.room.util.RoomType;
import net.wofly.common.util.CommonStatus;
import net.wofly.common.util.Identities;
import net.wofly.common.util.ResultCode;
import net.wofly.common.util.ReturnBean;
import net.wofly.right.domain.User;
import net.wofly.right.service.IUserService;

@Service("iRoomService")
@Transactional
public class RoomServiceImpl implements IRoomService {

    @Autowired
    RoomRepository roomRepository;

    @Autowired
    IUserService iUserService;

    @Override
    public Room save(Room entity) {
        return roomRepository.save(entity);
    }

    @Override
    public void delete(Room entity) {
        roomRepository.delete(entity);
    }

    @Override
    public void delete(String key) {
        roomRepository.delete(key);
    }

    @Override
    public Room findOne(String key) {
        return roomRepository.findOne(key);
    }

    @Override
    public List<Room> save(List<Room> entityList) {
        return (List<Room>) roomRepository.save(entityList);
    }

    @Override
    public List<Room> findByStatus(Integer status) {
        return roomRepository.findByStatus(status);
    }

    @Override
    public List<Room> findAll() {
        return (List<Room>) roomRepository.findAll();
    }

    @Override
    public ReturnBean<Room> addRoom(Room room, String employeeID) {

        if (!validateOperaterPrivaleges(employeeID, "addRoom")) {
            return new ReturnBean<Room>(ResultCode.ERROR_SERVICE, "权限不足，不能进行添加操作");
        }

        room.setRoomID(Identities.uuid2());
        room.setModifyDate(System.currentTimeMillis());
        room.setStatus(1);
        return new ReturnBean<Room>(ResultCode.SUCCESS, "添加成功", save(room));
    }

    @Override
    public ReturnBean<String> delRoom(String roomID, String employeeID) {

        if (!validateOperaterPrivaleges(employeeID, "delRoom")) {
            return new ReturnBean<String>(ResultCode.ERROR_SERVICE, "权限不足，不能进行删除操作");
        }

        Room room = findOne(roomID);
        if (room == null) {
            return new ReturnBean<String>(ResultCode.ERROR_SERVICE, "roomID不正确，未找到对应的房间信息");

        }
        room.setModifyDate(System.currentTimeMillis());
        room.setStatus(0);
        save(room);
        return new ReturnBean<String>(ResultCode.SUCCESS, "删除成功");
    }

    @Override
    public ReturnBean<List<Room>> getAllRoom(Integer status, String employeeID) {

        if (!validateOperaterPrivaleges(employeeID, "getAllRoom")) {
            return new ReturnBean<List<Room>>(ResultCode.ERROR_SERVICE, "权限不足，不能进行查询操作");
        }

        List<Room> roomList = null;
        if (status.intValue() == -1) {//查询所有的房间信息
            roomList = findAll();
        } else {
            roomList = findByStatus(status);
        }
        if (!CollectionUtils.isEmpty(roomList)) {
            for (Room room : roomList) {
                room.setRoomTypeStr(RoomType.getName(room.getRoomType()));
                room.setStatusStr(CommonStatus.getName(room.getStatus()));
            }
        }
        return new ReturnBean<List<Room>>(ResultCode.SUCCESS, "获取成功", roomList);
    }

    @Override
    public ReturnBean<Room> updateRoom(Room room, String employeeID) {

        if (!validateOperaterPrivaleges(employeeID, "updateRoom")) {
            return new ReturnBean<Room>(ResultCode.ERROR_SERVICE, "权限不足，不能进行更新操作");
        }

        Room dbRoom = findOne(room.getRoomID());
        if (dbRoom == null) {
            return new ReturnBean<Room>(ResultCode.ERROR_SERVICE, "未查询到对应的房间信息，房间ID不正确");
        }

        room.setModifyDate(System.currentTimeMillis());

        return new ReturnBean<Room>(ResultCode.SUCCESS, "添加成功", save(room));
    }

    private boolean validateOperaterPrivaleges(String userID, String operation) {

        User user = iUserService.findOne(userID);
        if (user == null) {
            return false;
        }

        return true;
    }
}
